package com.tbs.easyhomemoneyserver.controller;

import com.tbs.easyhomemoneyserver.DO.SysUser;
import com.tbs.easyhomemoneyserver.form.UserCreateForm;
import com.tbs.easyhomemoneyserver.model.ResultVO;
import com.tbs.easyhomemoneyserver.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("user")
@Api(tags = "用户接口")
public class UserController {

    @Resource
    IUserService userService;


    @PostMapping("register")
    @ApiOperation("用户注册")
    public ResultVO<SysUser> register(@RequestBody UserCreateForm createForm) {
        return ResultVO.success(userService.newUser(createForm));
    }

    @PostMapping("get")
    @ApiOperation("用户查询")
    public ResultVO<List<UserDTO>> find(@RequestBody BaseUserSearchForm searchForm) throws WarnException {
        if (searchForm == null) {
            throw new IllegalArgumentException("空表单提交");
        }
        List<SysUser> res = userService.findUserByCondition(searchForm);
        if (CollUtil.isEmpty(res)) {
            throw new RuntimeException("不存在的用户");
        }
        return ResultVO.success(DtoUtils.convertAll(res, UserDTO.class));
    }

    @PostMapping("login")
    @ApiOperation("登录")
    public ResultVO<LoginInfoDTO> login(@RequestBody LoginForm loginForm) {
        return ResultVO.success(userService.login(loginForm));
    }

    @GetMapping("test")
    @ApiOperation("测试接口")
    public ResultVO<String> register(String text) {
        return ResultVO.success(text);
    }
}
